<?php

namespace api\modules\mini\controllers;

use api\controllers\OnAuthController;
use api\modules\mini\forms\patient\PatientForm;
use api\modules\mini\forms\patient\PatientOrderForm;
use common\helpers\CommonHelper;
use common\models\spoker\Spoker;
use common\models\user\User;
use services\linkedcare\LinkedcarePatientsFeeDetailService;
use services\linkedcare\LinkedcarePatientsRecordService;
use services\patient\PatientOrderService;
use services\patient\PatientService;
use Yii;

/**
 *
 * Class PatientController
 * @package api\modules\v1\controllers
 */
class PatientController extends OnAuthController
{
    public $modelClass = '';

    public function beforeAction($action)
    {
        $res = parent::beforeAction($action); // TODO: Change the autogenerated stub

        $user = Yii::$app->user->identity->user;
        if ($user && $user->spoker && $user->spoker->status != Spoker::STATUS_SUCCESS) {
            CommonHelper::api(422, '代言人未审核通过，无权限查看');
            return false;
        }
        return $res;
    }

    /**
     * @api {get} /mini/patient/list  客户列表
     * @apiName list
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {String} name                  姓名
     * @apiParam {Number} page                  页码
     * @apiParam {Number} pageSize              每页展示数
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.page          页码
     * @apiSuccess {Number}  data.pageSize      每页展示数
     * @apiSuccess {Number}  data.total         总数
     * @apiSuccess {Array}   data.list          列表数据
     * @apiSuccess {Number}  data.list.id                   ID
     * @apiSuccess {String}  data.list.name                 姓名
     * @apiSuccess {String}  data.list.treatment_project    项目
     * @apiSuccess {String}  data.list.type_str             类型
     * @apiSuccess {String}  data.list.remark               备注
     * @apiSuccess {String}  data.list.created_at_str       创建时间
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new PatientService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        return $data;
    }

    /**
     * @api {get} /mini/patient/order-list  预约列表
     * @apiName order-list
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {String} name                  姓名
     * @apiParam {Number} page                  页码
     * @apiParam {Number} pageSize              每页展示数
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.page          页码
     * @apiSuccess {Number}  data.pageSize      每页展示数
     * @apiSuccess {Number}  data.total         总数
     * @apiSuccess {Array}   data.list          列表数据
     * @apiSuccess {Number}  data.list.id                   ID
     * @apiSuccess {String}  data.list.name                 姓名
     * @apiSuccess {String}  data.list.shop_name            门店
     * @apiSuccess {String}  data.list.treatment_project    项目
     * @apiSuccess {String}  data.list.type_str             类型
     * @apiSuccess {String}  data.list.created_at_str       创建时间
     * @apiSuccess {String}  data.list.order_time_str       预约时间
     */
    public function actionOrderList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new PatientOrderService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        return $data;
    }

    /**
     * @api {get} /mini/patient/arrive-list  到店列表
     * @apiName arrive-list
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {String} name                  姓名
     * @apiParam {Number} page                  页码
     * @apiParam {Number} pageSize              每页展示数
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.page          页码
     * @apiSuccess {Number}  data.pageSize      每页展示数
     * @apiSuccess {Number}  data.total         总数
     * @apiSuccess {Array}   data.list          列表数据
     * @apiSuccess {Number}  data.list.id                   ID
     * @apiSuccess {String}  data.list.name                 姓名
     * @apiSuccess {String}  data.list.shop_name            门店
     * @apiSuccess {String}  data.list.doctor_name          医生
     * @apiSuccess {String}  data.list.consultant_name      咨询
     * @apiSuccess {String}  data.list.treatment_project    项目
     * @apiSuccess {String}  data.list.type_str             类型
     * @apiSuccess {String}  data.list.arrive_time_str      预约时间
     */
    public function actionArriveList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new LinkedcarePatientsRecordService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        return $data;
    }

    /**
     * @api {get} /mini/patient/consume-list  消费列表
     * @apiName consume-list
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {String} name                  姓名
     * @apiParam {Number} page                  页码
     * @apiParam {Number} pageSize              每页展示数
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.page          页码
     * @apiSuccess {Number}  data.pageSize      每页展示数
     * @apiSuccess {Number}  data.total         总数
     * @apiSuccess {Array}   data.list          列表数据
     * @apiSuccess {Number}  data.list.id                   ID
     * @apiSuccess {String}  data.list.name                 姓名
     * @apiSuccess {String}  data.list.shop_name            门店
     * @apiSuccess {String}  data.list.actual_price         消费金额
     * @apiSuccess {String}  data.list.treatment_project    项目
     * @apiSuccess {String}  data.list.type_str             类型
     * @apiSuccess {String}  data.list.pay_time_str         消费时间
     */
    public function actionConsumeList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new LinkedcarePatientsFeeDetailService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        return $data;
    }

    /**
     * @api {get} /mini/patient/info  患者信息
     * @apiName info
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {String} id                    患者ID
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.id                ID页码
     * @apiSuccess {String}  data.name              姓名
     * @apiSuccess {Number}  data.mobile            手机号
     * @apiSuccess {String}  data.type_str          类型
     * @apiSuccess {Number}  data.discount_rate     折扣
     * @apiSuccess {String}  data.remark            备注
     * @apiSuccess {Number}  data.order_id          预约ID【=0 新增、>0 编辑】
     * @apiSuccess {Number}  data.arrive_num        到店数
     * @apiSuccess {Number}  data.sale_money        消费额
     * @apiSuccess {String}  data.created_at_str    创建时间
     * @apiSuccess {String}  data.created_at_str    创建时间
     * @apiSuccess {Array}   data.record_list       消费、预约记录
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        $service = new PatientService();
        // 查询结果
        return $service->getInfo((int)$id);
    }

    /**
     * @api {get} /mini/patient/edit-info  患者编辑信息
     * @apiName edit-info
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {String} id                    患者ID
     *
     * @apiSuccess {Object}  data                               返回数据
     * @apiSuccess {Number}  data.id                            ID
     * @apiSuccess {String}  data.name                          姓名
     * @apiSuccess {Number}  data.mobile                        手机号
     * @apiSuccess {Number}  data.type_id                       类型ID
     * @apiSuccess {Number}  data.discount_rate                 折扣
     * @apiSuccess {Array}   data.template_ids                  订阅模板IDs【id=0返回该参数】
     * @apiSuccess {Object}  data.condition                     筛选项
     * @apiSuccess {Array}   data.condition.treatment_project   项目
     * @apiSuccess {Array}   data.condition.shop                门店
     * @apiSuccess {Array}   data.condition.patient_type        类型
     */
    public function actionEditInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        $service = new PatientService();
        // 查询结果
        return $service->getEditInfo((int)$id);
    }

    /**
     * @api {get} /mini/patient/edit-order-info  患者编辑预约信息
     * @apiName edit-order-info
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {String} id                    预约ID
     *
     * @apiSuccess {Object}  data                           返回数据
     * @apiSuccess {Number}  data.id                        ID
     * @apiSuccess {Number}  data.order_time                预约时间
     * @apiSuccess {Number}  data.shop_id                   门店ID
     * @apiSuccess {Number}  data.treatment_project_id      项目ID
     * @apiSuccess {Number}  data.remark                    备注
     * @apiSuccess {Object}  data.condition                 筛选项
     * @apiSuccess {Array}   data.condition.treatment_project 项目
     * @apiSuccess {Array}   data.condition.shop              门店
     */
    public function actionEditOrderInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        $service = new PatientOrderService();
        // 查询结果
        return $service->getEditInfo((int)$id);
    }

    /**
     * @api {post} /mini/patient/create          登记患者
     * @apiName create
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {String} name                  姓名
     * @apiParam {Number} mobile                手机号
     * @apiParam {Number} type_id               类型ID
     * @apiParam {String} order_time            预约时间
     * @apiParam {Number} treatment_project_id  项目ID
     * @apiParam {Number} shop_id               门店ID
     * @apiParam {Number} discount_rate         折扣
     * @apiParam {String} remark                备注
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.id            患者ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();

        $model = new PatientForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new PatientService();
        $service->create($model);
        return true;
    }

    /**
     * @api {post} /mini/patient/update         编辑患者
     * @apiName update
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {Number} id                    患者ID
     * @apiParam {String} name                  姓名
     * @apiParam {Number} mobile                手机号
     * @apiParam {Number} type_id               类型ID
     * @apiParam {Number} discount_rate         折扣
     * @apiParam {String} remark                备注
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.id            患者ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();

        $model = new PatientForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new PatientService();
        $service->update($model);
        return true;
    }

    /**
     * @api {post} /mini/patient/create-order   新增预约
     * @apiName create-order
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {String} patient_id            患者ID
     * @apiParam {String} order_time            预约时间
     * @apiParam {Number} treatment_project_id  项目ID
     * @apiParam {Number} shop_id               门店ID
     * @apiParam {String} remark                备注
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.id            患者ID
     */
    public function actionCreateOrder()
    {
        $params = Yii::$app->request->post();

        $model = new PatientOrderForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new PatientOrderService();
        $service->create($model);
        return true;
    }

    /**
     * @api {post} /mini/patient/update-order   编辑预约
     * @apiName update-order
     * @apiGroup MiniPatient
     * @apiUse CommonParam
     *
     * @apiParam {Number} id                    预约ID
     * @apiParam {String} patient_id            患者ID
     * @apiParam {String} order_time            预约时间
     * @apiParam {Number} treatment_project_id  项目ID
     * @apiParam {Number} shop_id               门店ID
     * @apiParam {String} remark                备注
     *
     * @apiSuccess {Object}  data               返回数据
     * @apiSuccess {Number}  data.id            患者ID
     */
    public function actionUpdateOrder()
    {
        $params = Yii::$app->request->post();

        $model = new PatientOrderForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            return CommonHelper::api(422, CommonHelper::analyErr($model->getFirstErrors()));
        }

        $service = new PatientOrderService();
        $service->update($model);
        return true;
    }

}
